﻿using System;
using System.Collections.Generic;
using Interfaces.Add_Interfaces;
using Interfaces.Information_Interfaces;
using Interfaces.General_Interfaces;
using Model.Domain_Model;
using Repository;
using Presenters.Inherited_Presenters;

namespace Presenters.Add_Presenters
{
    public class RoadNewPresenter:OperationStatus
    {
        IDb database = Repo.GetDatabaseInstance();
        IRoadAddView view = null;
        public Client client = null;

        public RoadNewPresenter(IRoadAddView view)
        {
            this.view = view;
        }

        /// <summary>
        /// Otwiera moduł wyszukiwania klienta w bazie danych
        /// </summary>
        public void SearchClientDisplay(ISearchClientView view)
        {
            view.SetParams(this.view);
            view.ReturnView<System.Windows.Forms.Form>().ShowDialog();
            SendDataToView();
        }

        /// <summary>
        /// Przesyła dane do widoku
        /// </summary>
        public void SendDataToView()
        {
            view.clientNameCorporation = (client != null) ? client.nameCorporation : String.Empty;
        }

        /// <summary>
        /// Pobiera wszystkie rodzaje walut
        /// </summary>
        public void GetCurrencyList()
        {
            IList<Currency> currencyList = new List<Currency>();
            currencyList.Add(new Currency { idCurrency = 1, actuallyValue = 1, nameCurrency = "Waluta Euro", shortcut = "EUR", dateSync = DateTime.Now });
            currencyList.Add(new Currency { idCurrency = 2, actuallyValue = 1, nameCurrency = "Polski Złoty", shortcut = "PLN", dateSync = DateTime.Now });
            view.currencyList = currencyList;
        }

        public void GetTypeRoadList()
        {
            IList<TypeRoad> typeRoadList = new List<TypeRoad>();
            typeRoadList.Add(new TypeRoad { idType = 1, nameType = "Standardowy" });
            typeRoadList.Add(new TypeRoad { idType = 2, nameType = "Płynny - spożwyczy" });
            view.typeRoadList = typeRoadList;
        }

        /// <summary>
        /// Zapisuje do tabeli Roads, Valuations obiekty typu Road, Valuation składające się na wycenę
        /// </summary>
        public void Save()
        {
            nameOperation = "Dodawanie wyceny";
            try
            {
                database.Roads.NewRoad(
                    new Road { fromCity = view.fromCity, fromCountry = view.fromCountry, idClient = client.idClient, idType = view.type.idType, toCity = view.toCity, toCountry = view.toCountry },
                    new Valuation { dateModify = DateTime.Now, idCurrency = view.currency.idCurrency, isActually = true, value = view.value });
                view.ShowSuccess();
                view.Close();
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }
    }
}
